package edu.ucdavis.fiehnlab.alchemy.alchemyAdmin.snippet

/**
 * Created with IntelliJ IDEA.
 * User: naru
 * Date: 4/2/13
 * Time: 3:05 PM
 * To change this template use File | Settings | File Templates.
 */

import net.liftweb._
import util._
import Helpers._

import common._

import sitemap._
import Loc._
import net.liftweb._
import scala.xml._

import net.liftweb.http._
import net.liftweb.json._

import net.liftweb.http.rest._
import net.liftweb.json.JsonAST._
import net.liftweb.util.Helpers
import net.liftweb.json.JsonAST.JValue
import net.liftweb.http.rest.RestHelper
import net.liftweb.mapper.By
import edu.ucdavis.fiehnlab.alchemy.alchemyAdmin.model._


/**
 *
 * Implementing rest Web Services for Methods and Compounds
 */
object WebServices extends RestHelper {
  /*

  /**
   * Rest api webServices for Methods vis /webServices/method
   */
  serve ( "webServices" / "method" prefix {
    /**
     * return all Methods on /webServices/method
     */
    case Nil JsonGet _ => Method.fieldByName("name") : JValue

    /**
     * return all Methods on /webServices/method/list
     */

    case "list" :: Nil JsonGet _ => Method.fieldByName("name") : JValue

    /**
     *
     * return Method on id search /webServices/method/45
     */
    case Method(method) :: Nil JsonGet _ => Nil : JValue

    /**
     * search methods using
     * /webServices/method/search/searchTerm or /webServices/method/search?q=searchTerm
     */
    case "search" :: q JsonGet _ =>
    (for {
      searchString <- q ::: S.params("q")
      method <- Method.search(searchString)

    /**
     * distinct records constraint
     */
    } yield method).distinct : JValue

    /**
     * delete Method using webService
     */
    case Method(method) :: Nil JsonDelete _ =>
      Method.delete(method.id).map(a => a:JValue)

    /**
     * add new Method using Rest Api webServices
     */


    case Nil JsonPut Method(method) -> _ => Method.add(method): JValue

    /**
     *
     * update the method using rest api webservices
     */



    case Method(method) :: Nil JsonPst json -> _ => Method(mergeJson(method,json)).map(Method.add(_):JValue)


  })


  /**
   * Rest api webServices for Compounds
   */

  serve ( "webServices" / "compound" prefix {
    /**
     * return all Compounds on /webServices/compound
     */
    case Nil JsonGet _ => Compound.fieldByName("compoundName") : JValue

    /**
     * return all Compounds on /webServices/compound/list
     */

    case "list" :: Nil JsonGet _ => Compound.fieldByName("name") : JValue

    /**
     *
     * return compound on id search /webServices/compound/45
     */
    case Compound(compound) :: Nil JsonGet _ => Nil : JValue

    /**
     * search Compounds using
     * /webServices/compound/search/searchTerm or /webServices/compound/search?q=searchTerm
     */
    case "search" :: q JsonGet _ =>
      (for {
        searchString <- q ::: S.params("q")
        compound <- Compound.search(searchString)

      /**
       * distinct records constraint
       */
      } yield compound).distinct : JValue

    /**
     * give a list of internal Standards  via /webServices/compound/internalStandard
     */
    case "internalStandard" :: Nil JsonGet _ =>
      ( for {
      compound <- Compound.findAll(By(Compound.internalStandard,true))
      }yield compound).distinct : JValue
    /**
     * delete Compound using webService
     */
    case Compound(compound) :: Nil JsonDelete _ =>
      Compound.delete(compound.id).map(a => a:JValue)

    /**
     * add new Compound using Rest Api webServices
     */


    case Nil JsonPut Compound(compound) -> _ => Compound.add(compound): JValue

    /**
     *
     * update the compound using rest api webServices
     */

    case Compound(compound) :: Nil JsonPst json -> _ =>
      Compound(mergeJson(compound,json)).map(Compound.add(_):JValue)


  })   */

}
